<?php
    /**
     * User zgh
     * Email 1803897171@qq.com
     * Date 2021/7/13
     **/

    namespace app\api\validate;


    use app\api\service\LoginService;
    use think\Validate;

    class LoginValidate extends Validate
    {
        const SEND_SMS = 'send_sms';
        const LOGIN_TYPE = 'login_type';

        protected $rule = [
            'mobile|手机号' => 'require|mobile',
            'login_type|登陆方式' => 'loginType',
        ];

        protected $scene = [
            'send_sms' => ['mobile'],
            'login_type' => ['login_type'],
        ];


        protected function loginType($value, $rule, $data = [])
        {
            if (empty($data['login_type']) || !is_numeric($data['login_type']) || !in_array($data['login_type'], [LoginService::LOGIN_TYPE_CODE, LoginService::LOGIN_TYPE_PASSWORD])) {
                return '登陆方式错误';
            }
            if (empty($data['mobile'])) {
                return '手机号不能为空';
            }
            if ($data['login_type'] == LoginService::LOGIN_TYPE_CODE) {
                if (empty($data['verify_code'])) {
                    return '验证码不能为空';
                } else {
                    return true;
                }
            } else {
                if (empty($data['password'])) {
                    return '密码不能为空';
                } else {
                    return true;
                }
            }
        }


    }